package runnables;

import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.ui.TransitionContext;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import screens.Controller;
import strings.WebcampusResource;
import test.GradeAnalyzer;
import beans.Course;

public class GroupRunnable implements Runnable, WebcampusResource{

	private static ResourceBundle resources = ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);

	private Course course;
	private String hash;

	private boolean isRefresh;

	public GroupRunnable(Course c, String hash, boolean refresh){
		this.course=c;
		this.hash=hash;
		this.isRefresh=refresh;
	}

	public void run() {
		//if (course.getGroup()==null){
		GradeAnalyzer ga = GradeAnalyzer.getInstance();
		try{
			ga.getGrupo(course, hash);
		} catch (Exception e){
			System.out.println("********************** error while getting group info from webservice");
			//}
		}

		//if everything is ok, open the following screen
		UiApplication.getUiApplication().invokeLater(new Runnable() {
			public void run() {
				if (isRefresh){
					//UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());
					Controller.getInstance().openGroupMessagesScreen(course,TransitionContext.TRANSITION_WIPE);
					System.out.println("********************* Opening group message refresh screen");
				} else {
					if (course.getGroup()!=null){
						Controller.getInstance().openGroupsScreen(course);
						System.out.println("********************* Opening group screen");
					} else {
						Dialog.alert(resources.getString(ALERT_NOGROUP));
					}
				}
			}
		});
	}

}
